package com.moss.cloud.remote.biz.controller;

import com.moss.cloud.common.core.constant.ServiceList;
import com.moss.cloud.common.core.model.Result;
import com.moss.cloud.remote.api.model.from.WXUserInfoFrom;
import com.moss.cloud.remote.api.model.from.WXUserTokenFrom;
import com.moss.cloud.remote.biz.service.IWXService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信接口控制器
 *
 * @author 瑾年
 * @date 2023年3月31日
 */
@RestController
@RequestMapping(ServiceList.REMOTE + "/wx")
public class WXController {


    private IWXService wxService;
    public WXController(IWXService wxService){
        this.wxService = wxService;
    }

    /**
     * 用CODE换取access_token
     *
     * @param wxUserTokenFrom
     * @return
     */
    @PostMapping("/doUserToken")
    public Result<?> doGetToken(@RequestBody @Validated WXUserTokenFrom wxUserTokenFrom) {
        return Result.success(wxService.doGetToken(wxUserTokenFrom));
    }

    /**
     * 用accessToken和openid获取用户信息
     *
     * @param wxUserInfoFrom
     * @return
     */
    @PostMapping("/doUserInfo")
    public Result<?> doGetUserInfo(@RequestBody @Validated WXUserInfoFrom wxUserInfoFrom) {
        return Result.success(wxService.doGetUserInfo(wxUserInfoFrom));
    }

    /**
     * JS_CODE获取openid
     *
     * @param wxUserTokenFrom
     * @return
     */
    @PostMapping("/doOpenId")
    public Result<?> doOpenId(@RequestBody @Validated WXUserTokenFrom wxUserTokenFrom) {
        return Result.success(wxService.doGet(wxUserTokenFrom));
    }
}
